package com.zh.Service;

import com.deepoove.poi.XWPFTemplate;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

public class ReadWord {

    public static void main(String[] args) throws IOException {
        String filePath1 = "E://test.docx";
        String filePath2 = "E://test2.docx";
        XWPFDocument doc = new XWPFDocument(new FileInputStream(filePath2));
        XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
//        String text = extractor.getText();
//        System.out.println(text);
        List<IBodyElement> bodyElements = doc.getBodyElements();
        List<XWPFPictureData> allPictures = doc.getAllPictures();

        /**
         * {@link  org.apache.poi.xwpf.usermodel.IBodyElement}
         * 该接口表示word中元素顶级接口,有三个实现类,分别表示 段落  表格 SDT元素
         * */
        for (IBodyElement bodyElement : bodyElements) {
            if (bodyElement instanceof XWPFParagraph) { //如果是段落
                XWPFParagraph par = (XWPFParagraph) bodyElement;
                par.getText();
                System.out.println(par.getText());

            }
            if (bodyElement instanceof XWPFTable) {//如果是表格
                XWPFTable par = (XWPFTable) bodyElement;
                List<XWPFTableRow> rows = par.getRows();
                System.out.println(par.getText());
            }
            if (bodyElement instanceof XWPFSDT) {//如果是SDT元素,虽然没搞清楚这是个啥


            }

        }
    }
    }
